<template>
  <div class="dashboard-container">
    <div class="hd">
      <div class="title">
        <svg-icon icon-class="size" />字体处理</div>
    </div>
    <div class="bd">
      <div class="item font-set">
        <div class="cartoon-button" @click="setFont('普通对话')">普通对话</div>
        <div class="button-group">
          <div class="cartoon-button" @click="setFont('内心话、旁白')">内心话、旁白</div>
          <div class="cartoon-button" @click="setFont('内心话、旁白白边')">白边</div>
          <div class="cartoon-button" @click="setFont('内心话、旁白黑边')">黑边</div>
        </div>
        <div class="cartoon-button" @click="setFont('大声喊')">大声喊</div>
        <div class="button-group">
          <div class="cartoon-button" @click="setFont('效果音')">效果音</div>
          <div class="cartoon-button" @click="setFont('效果音白边')">白边</div>
          <div class="cartoon-button" @click="setFont('效果音黑边')">黑边</div>
        </div>
        <div class="button-group">
          <div class="cartoon-button" @click="setFont('框外对话、手写')">框外对话、手写</div>
          <div class="cartoon-button" @click="setFont('框外对话、手写白边')">白边</div>
          <div class="cartoon-button" @click="setFont('框外对话、手写黑边')">黑边</div>
        </div>
        <div class="cartoon-button" @click="setFont('电话、短信、广播')">电话、短信、广播</div>
        <div class="cartoon-button" @click="setFont('回想、过去对话')">回想、过去对话</div>
        <div class="cartoon-button" @click="setFont('过去大声喊')">过去大声喊</div>
        <div class="cartoon-button" @click="setFont('古书古典字')">古书古典字</div>
        <div class="button-group">
          <div class="cartoon-button" @click="setFont('地名时间')">地名时间</div>
          <div class="cartoon-button" @click="setFont('地名时间白边')">白边</div>
          <div class="cartoon-button" @click="setFont('地名时间黑边')">黑边</div>
        </div>
        <div class="cartoon-button" @click="setFont('牌匾横幅')">牌匾横幅</div>
      </div>
    </div>
    <div class="hd">
      <div class="title">
        <svg-icon icon-class="message" />气泡框处理</div>
    </div>
    <div class="bd">
      <div class="item">
        <el-radio v-model="border.set" label="[0,0,0]" @change="changeBorderSet()">普通</el-radio>
        <el-radio v-model="border.set" label="[61,41,23]" @change="changeBorderSet()">武器店大叔</el-radio>
      </div>
    </div>
    <div class="bd">
      <div class="item checkbox">
        <el-checkbox v-model="autoBorder" @change="autoBorderScript()">自动</el-checkbox>
        <div class="cartoon-button" @click="setLayerBorder('气泡框描边')">气泡框描边</div>
      </div>
    </div>
    <div class="bd">
      <div class="item">
        <el-slider v-model="angle" :show-tooltip="false"></el-slider>
        <el-slider v-model="random" :show-tooltip="false"></el-slider>
      </div>
      <div class="item button-group">
        <div class="cartoon-button" @click="makeDialg(1)">爆炸框</div>
        <div class="cartoon-button" @click="makeDialg(2)">泡泡</div>
        <div class="cartoon-button" @click="makeDialg(3)">手绘</div>
      </div>
    </div>

    <div class="hd">
      <div class="title">
        <svg-icon icon-class="dashboard" />批处理</div>
    </div>
    <div class="bd">
      <div class="item">
        <el-checkbox label="归类文件夹" v-model="autoList.autoFolder"></el-checkbox>
        <el-checkbox label="调整效果音大小及行高" v-model="autoList.autoLineHeight"></el-checkbox>
        <el-checkbox label="删除文本末尾空格" v-model="autoList.deleteSpace"></el-checkbox>
        <el-checkbox label="保存jpg文件" v-model="autoList.saveJPG"></el-checkbox>
      </div>
    </div>
    <div class="bd">
      <div class="item">
        <div class="cartoon-button" @click="autoScript()">开始处理</div>
      </div>
    </div>
    <div class="bd">
      <div class="item">
        <div class="cartoon-button" @click="zipPSD()">优化文件大小并保存</div>
      </div>
    </div>
    <div class="bd" style="height: 40px; border-bottom: none;">
      <div class="item">
        {{processMessage}}
      </div>
    </div>

  </div>
</template>

<script>

import {
    psScrpit,
    psEvent
  } from 'adobe/CSInterface'

  import {
    addUsed
  } from '@/api/used'

  export default {
    name: 'Font',
    data() {
      return {
        angle:50,
        random:0,
        autoBorder: false,
        autoList: {
          autoFolder: true,
          autoLineHeight: true,
          deleteSpace: true,
          saveJPG: true
        },
        fontList: [{
            name: '普通对话',
            size: 16,
            style: '浑厚',
            color: '000000',
            line_height: 'auto',
            bold: false,
            italic: false,
            font_familay: 'NanumMyeongjo',
            horizontalScale: 95,
            clear_layer_style: true,
            border: null
          },
          {
            name: '大声喊',
            size: 18,
            style: '犀利',
            color: '000000',
            line_height: 'auto',
            bold: false,
            italic: true,
            font_familay: 'NanumGothicBold',
            horizontalScale: 95,
            clear_layer_style: true,
            border: null
          },
          {
            name: '效果音',
            size: 40,
            style: '平滑',
            color: '000000',
            line_height: 40,
            bold: false,
            italic: false,
            font_familay: 'NanumBrush',
            horizontalScale: 100,
            clear_layer_style: true,
            border: null
          },
          {
            name: '效果音白边',
            size: 40,
            style: '平滑',
            color: '000000',
            line_height: 40,
            bold: false,
            italic: false,
            font_familay: 'NanumBrush',
            horizontalScale: 100,
            clear_layer_style: false,
            border: {
              width: 8,
              color: [255, 255, 255]
            }
          },
          {
            name: '效果音黑边',
            size: 40,
            style: '平滑',
            color: 'ffffff',
            line_height: 40,
            bold: false,
            italic: false,
            font_familay: 'NanumBrush',
            horizontalScale: 100,
            clear_layer_style: false,
            border: {
              width: 8,
              color: [0, 0, 0]
            }
          },
          {
            name: '内心话、旁白',
            size: 16,
            style: '浑厚',
            color: '000000',
            line_height: 'auto',
            bold: false,
            italic: false,
            font_familay: 'NanumGothic',
            horizontalScale: 95,
            clear_layer_style: true,
            border: null
          },
          {
            name: '内心话、旁白白边',
            size: 16,
            style: '浑厚',
            color: '000000',
            line_height: 'auto',
            bold: false,
            italic: false,
            font_familay: 'NanumGothic',
            horizontalScale: 95,
            clear_layer_style: false,
            border: {
              width: 8,
              color: [255, 255, 255]
            }
          },
          {
            name: '内心话、旁白黑边',
            size: 16,
            style: '浑厚',
            color: 'ffffff',
            line_height: 'auto',
            bold: false,
            italic: false,
            font_familay: 'NanumGothic',
            horizontalScale: 95,
            clear_layer_style: false,
            border: {
              width: 8,
              color: [0, 0, 0]
            }
          },
          {
            name: '框外对话、手写',
            size: 22,
            style: '平滑',
            color: '000000',
            line_height: 22,
            bold: false,
            italic: false,
            font_familay: 'NanumPen',
            horizontalScale: 100,
            clear_layer_style: true,
            border: null
          },
          {
            name: '框外对话、手写白边',
            size: 22,
            style: '平滑',
            color: '000000',
            line_height: 22,
            bold: false,
            italic: false,
            font_familay: 'NanumPen',
            horizontalScale: 100,
            clear_layer_style: false,
            border: {
              width: 8,
              color: [255, 255, 255]
            }
          },
          {
            name: '框外对话、手写黑边',
            size: 22,
            style: '平滑',
            color: 'ffffff',
            line_height: 22,
            bold: false,
            italic: false,
            font_familay: 'NanumPen',
            horizontalScale: 100,
            clear_layer_style: false,
            border: {
              width: 8,
              color: [0, 0, 0]
            }
          },
          {
            name: '电话、短信、广播',
            size: 16,
            style: '犀利',
            color: '000000',
            line_height: 'auto',
            bold: false,
            italic: false,
            font_familay: 'YiSunShinDotumL',
            horizontalScale: 100,
            clear_layer_style: true,
            border: null
          },
          {
            name: '回想、过去对话',
            size: 16,
            style: '浑厚',
            color: '000000',
            line_height: 'auto',
            bold: false,
            italic: false,
            font_familay: 'Daehan',
            horizontalScale: 95,
            clear_layer_style: true,
            border: null
          },
          {
            name: '过去大声喊',
            size: 18,
            style: '犀利',
            color: '000000',
            line_height: 'auto',
            bold: false,
            italic: true,
            font_familay: 'Daehan-Bold',
            horizontalScale: 95,
            clear_layer_style: true,
            border: null
          },
          {
            name: '古书古典字',
            size: 16,
            style: '浑厚',
            color: '000000',
            line_height: 'auto',
            bold: false,
            italic: false,
            font_familay: 'NanumMyeongjoExtraBold',
            horizontalScale: 95,
            clear_layer_style: true,
            border: null
          },
          {
            name: '地名时间',
            size: 16,
            style: '浑厚',
            color: '000000',
            line_height: 'auto',
            bold: false,
            italic: false,
            font_familay: 'NanumBarunGothic',
            horizontalScale: 95,
            clear_layer_style: true,
            border: null
          },
          {
            name: '地名时间白边',
            size: 16,
            style: '浑厚',
            color: '000000',
            line_height: 'auto',
            bold: false,
            italic: false,
            font_familay: 'NanumBarunGothic',
            horizontalScale: 95,
            clear_layer_style: false,
            border: {
              width: 8,
              color: [255, 255, 255]
            }
          },
          {
            name: '地名时间黑边',
            size: 16,
            style: '浑厚',
            color: 'ffffff',
            line_height: 'auto',
            bold: false,
            italic: false,
            font_familay: 'NanumBarunGothic',
            horizontalScale: 95,
            clear_layer_style: false,
            border: {
              width: 8,
              color: [0, 0, 0]
            }
          },
          {
            name: '牌匾横幅',
            size: 16,
            style: '浑厚',
            color: '000000',
            line_height: 'auto',
            bold: false,
            italic: false,
            font_familay: 'NanumSquareRoundEB',
            horizontalScale: 95,
            clear_layer_style: true,
            border: null
          }
        ],
        border: {
          set: '[0,0,0]',
          background: [255, 255, 255],
          width: 4,
          color: [0, 0, 0]
        },
        processMessage: '',
      }
    },
    created() {
      //开启流程监听
      var that = this;
      psScrpit.addEventListener("sendProcessMessage", function(Event) {
        that.processMessage = JSON.stringify(Event.data);
      });
    },
    destroyed() {
      //关闭流程监听
      psScrpit.removeEventListener("sendProcessMessage");
    },
    methods: {
      makeDialg(type){
        switch(type) {
          case 1:
            psScrpit.evalScript('makeDialg('+this.angle+',1,'+this.random+')');
            break;
          case 2:
            psScrpit.evalScript('makeDialg('+this.angle+',2,'+this.random+')');
            break;
          case 3:
          var num = parseInt(Math.random() * ( this.random - 0 + 1 ) + 0, 10)+this.angle;
          psScrpit.evalScript('makeHandDialg('+num+')');
            break;
        }
      },
      zipPSD() {
        this.processMessage = '开始处理...';
        psScrpit.evalScript('getDocInfo()', function(info) {
          //提交记录
          var psd = JSON.parse(info);
          var data = {
            category: 'savepsd',
            doc_name: psd.name,
            doc_id: psd.id
          }
          addUsed(data).then(response => {
            if (response) {

            } else {
              console.log('error')
              return false
            }
          }).catch((error) => {
            console.log(error)
          })
        });
        psScrpit.evalScript('deleteDocumentAncestorsMetadata()');
      },
      changeBorderSet() {
        this.border.color = JSON.parse(this.border.set);
        this.stopAutoBorderScript();
      },
      stopAutoBorderScript() {
        psEvent.type = "com.adobe.PhotoshopUnRegisterEvent"; //创建一个“取消注册事件”
        psEvent.scope = "APPLICATION"; // 限定在宿主应用范围内
        psEvent.extensionId = psScrpit.getExtensionID(); //设置“取消注册事件” extensionId 为你扩展的扩展ID
        psEvent.data = "1298866208"; // 要取消注册事件的 id
        psScrpit.dispatchEvent(psEvent); //发送“取消注册事件”，取消注册
        if (this.autoBorder) {
          this.autoBorderScript();
        }
      },
      autoBorderScript() {
        var data = JSON.stringify(this.border);
        if (this.autoBorder) {
          psEvent.type = "com.adobe.PhotoshopRegisterEvent";
          psEvent.scope = "APPLICATION"; // 限定在宿主应用范围内
          psEvent.extensionId = psScrpit.getExtensionID(); //设置“注册事件” extensionId 为你扩展的扩展ID
          psEvent.data = "1298866208"; // 想要监听的 ExtendScript 事件 eventID , 这里是创建图层对象事件: "Mk  "=1298866208
          psScrpit.dispatchEvent(psEvent); //发送“注册事件”，完成注册
          psScrpit.addEventListener("com.adobe.PhotoshopJSONCallback" + psScrpit.getExtensionID(), function(event) {
            psScrpit.evalScript('autoBorderScript(' + data + ')');
          })
        } else {
          this.stopAutoBorderScript();
        }
      },
      autoScript() {
        this.processMessage = '开始处理...';
        psScrpit.evalScript('getDocInfo()', function(info) {
          //提交记录
          var psd = JSON.parse(info);
          var data = {
            category: 'autofilter',
            doc_name: psd.name,
            doc_id: psd.id
          }
          addUsed(data).then(response => {
            if (response) {

            } else {
              console.log('error')
              return false
            }
          }).catch((error) => {
            console.log(error)
          })
        });
        psScrpit.evalScript('autoScript(' + JSON.stringify(this.autoList) + ')');
      },
      setLayerBorder(name) {
        psScrpit.evalScript('autoBorderScript(' + JSON.stringify(this.border) + ')');
      },
      setFont(name) {
        for (var i = 0; i < this.fontList.length; i++) {
          if (this.fontList[i].name === name) {
            var info = this.fontList[i]
            psScrpit.evalScript('setFontStyle(' + JSON.stringify(info) + ');')
          }
        }
      }
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss">
  .dashboard-container {
    .el-slider {
      width: 40%;
      margin: 0 5%;
      float: left;
    }
    .el-slider__bar {
      background: #999999;
    }
    .el-slider__runway {
      background: #4c4c4c;
    }
    .el-slider__button {
      border-color: #999999;
      background: #4c4c4c;
    }
  }
  .el-radio {
    .el-radio__inner {
      background: #999;
      border: 1px solid #141414;
    }
  }

  .el-radio.is-checked {
    .el-radio__inner {
      background: #999;
      border: 1px solid #141414;
    }

    .el-radio__label {
      color: #fff;
    }
  }

  .checkbox {
    .el-checkbox {
      float: left;
      width: 30%;
      padding: 5px 0;
    }

    .cartoon-button {
      float: left;
      width: 70%;
    }
  }

  .checkbox:after {
    content: '';
    display: table;
    clear: both;
  }

  .el-checkbox__input.is-checked+.el-checkbox__label {
    color: #ccc;
  }

  .el-checkbox__inner {
    background: #666;
    border-color: #141414;
  }

  .el-checkbox__input.is-checked .el-checkbox__inner,
  .el-checkbox__input.is-indeterminate .el-checkbox__inner {
    background-color: #333;
    border-color: #141414;
  }
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
  .dashboard {
    &-container {
      font-size: 12px;

      .hd {
        font-size: 12px;
        padding: 10px 15px;
        border-bottom: 1px solid #141414;
        border-top: 1px solid #454545;

        .svg-icon {
          margin-right: 5px;
        }
      }

      .bd {
        padding: 6px 15px;
        border-bottom: 1px solid #141414;
        border-top: 1px solid #454545;

        .cartoon-button {
          display: block;
          text-align: center;
          border: 1px solid #141414;
          height: 30px;
          line-height: 28px;
          padding: 0 10px;
          margin-bottom: 1px;
          border-radius: 4px;
          background: #4c4c4c;
          cursor: pointer;
        }

        .cartoon-button:hover {
          background: #222222;
        }

        .el-checkbox-group {
          margin-bottom: 10px;
        }

        .button-group {
          .cartoon-button {
            float: left;
            width: 25%;
          }

          .cartoon-button:first-child {
            float: left;
            width: 50%;
          }

          .cartoon-button:not(:last-child) {
            border-top-right-radius: 0;
            border-bottom-right-radius: 0;
            border-right: none;
          }

          .cartoon-button:not(:first-child) {
            border-top-left-radius: 0;
            border-bottom-left-radius: 0;
            border-left-color: #333;
          }
        }

        .button-group:after {
          content: '';
          display: table;
          clear: both;
        }
      }

      .font-set {
        .cartoon-button {
          text-align: left;
        }
      }
    }

    &-text {
      font-size: 14px;
      line-height: 46px;
    }

  }
</style>
